﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

public static class LayoutManager
{
    public static void SetThreeRegionLayoutTop(Grid mainGrid)
    {
        ClearMainGrid(mainGrid);

        // 嵌套的上部Grid
        var topGrid = new Grid();
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        CreateRegion(topGrid, "区域 1", 0, 0);
        CreateRegion(topGrid, "区域 2", 0, 2);

        AddGridSplitter(topGrid, 0, 1, GridResizeDirection.Columns);

        Grid.SetRow(topGrid, 0);
        mainGrid.Children.Add(topGrid);

        // 下部单个区域
        CreateRegion(mainGrid, "区域 3", 2, 0, 3);

        // 主分隔线在中间
        AddGridSplitter(mainGrid, 1, 0, GridResizeDirection.Rows);
    }

    public static void SetThreeRegionLayoutBottom(Grid mainGrid)
    {
        ClearMainGrid(mainGrid);

        // 上部单个区域
        CreateRegion(mainGrid, "区域 1", 0, 0, 3);

        // 嵌套的下部Grid
        var bottomGrid = new Grid();
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        CreateRegion(bottomGrid, "区域 2", 0, 0);
        CreateRegion(bottomGrid, "区域 3", 0, 2);

        AddGridSplitter(bottomGrid, 0, 1, GridResizeDirection.Columns);

        Grid.SetRow(bottomGrid, 2);
        mainGrid.Children.Add(bottomGrid);

        // 主分隔线在中间
        AddGridSplitter(mainGrid, 1, 0, GridResizeDirection.Rows);
    }

    public static void SetFourRegionLayout(Grid mainGrid)
    {
        ClearMainGrid(mainGrid);

        // 嵌套的上部Grid
        var topGrid = new Grid();
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        CreateRegion(topGrid, "区域 1", 0, 0);
        CreateRegion(topGrid, "区域 2", 0, 2);

        AddGridSplitter(topGrid, 0, 1, GridResizeDirection.Columns);

        Grid.SetRow(topGrid, 0);
        mainGrid.Children.Add(topGrid);

        // 嵌套的下部Grid
        var bottomGrid = new Grid();
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        CreateRegion(bottomGrid, "区域 3", 0, 0);
        CreateRegion(bottomGrid, "区域 4", 0, 2);

        AddGridSplitter(bottomGrid, 0, 1, GridResizeDirection.Columns);

        Grid.SetRow(bottomGrid, 2);
        mainGrid.Children.Add(bottomGrid);

        // 主分隔线在中间
        AddGridSplitter(mainGrid, 1, 0, GridResizeDirection.Rows);
    }

    public static void SetFiveRegionLayoutThreeOnTop(Grid mainGrid)
    {
        ClearMainGrid(mainGrid);

        // 上部嵌套Grid（3列）
        var topGrid = new Grid();
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        CreateRegion(topGrid, "区域 1", 0, 0);
        CreateRegion(topGrid, "区域 2", 0, 2);
        CreateRegion(topGrid, "区域 3", 0, 4);

        AddGridSplitter(topGrid, 0, 1, GridResizeDirection.Columns);
        AddGridSplitter(topGrid, 0, 3, GridResizeDirection.Columns);

        Grid.SetRow(topGrid, 0);
        mainGrid.Children.Add(topGrid);

        // 下部嵌套Grid（2列）
        var bottomGrid = new Grid();
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        CreateRegion(bottomGrid, "区域 4", 0, 0);
        CreateRegion(bottomGrid, "区域 5", 0, 2);

        AddGridSplitter(bottomGrid, 0, 1, GridResizeDirection.Columns);

        Grid.SetRow(bottomGrid, 2);
        mainGrid.Children.Add(bottomGrid);

        // 主分隔线在中间
        AddGridSplitter(mainGrid, 1, 0, GridResizeDirection.Rows);
    }

    public static void SetFiveRegionLayoutTwoOnTop(Grid mainGrid)
    {
        ClearMainGrid(mainGrid);

        // 上部嵌套Grid（2列）
        var topGrid = new Grid();
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
        topGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        CreateRegion(topGrid, "区域 1", 0, 0);
        CreateRegion(topGrid, "区域 2", 0, 2);

        AddGridSplitter(topGrid, 0, 1, GridResizeDirection.Columns);

        Grid.SetRow(topGrid, 0);
        mainGrid.Children.Add(topGrid);

        // 下部嵌套Grid（3列）
        var bottomGrid = new Grid();
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5, GridUnitType.Pixel) });
        bottomGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

        CreateRegion(bottomGrid, "区域 3", 0, 0);
        CreateRegion(bottomGrid, "区域 4", 0, 2);
        CreateRegion(bottomGrid, "区域 5", 0, 4);

        AddGridSplitter(bottomGrid, 0, 1, GridResizeDirection.Columns);
        AddGridSplitter(bottomGrid, 0, 3, GridResizeDirection.Columns);

        Grid.SetRow(bottomGrid, 2);
        mainGrid.Children.Add(bottomGrid);

        // 主分隔线在中间
        AddGridSplitter(mainGrid, 1, 0, GridResizeDirection.Rows);
    }

    private static void ClearMainGrid(Grid mainGrid)
    {
        mainGrid.Children.Clear();
    }

    private static void CreateRegion(Grid parentGrid, string name, int row, int column, int columnSpan = 1)
    {
        Border border = new Border
        {
            BorderBrush = Brushes.Transparent,
            BorderThickness = new Thickness(0),
            Background = Brushes.LightGray,
            Margin = new Thickness(5)
        };
        border.Child = new TextBlock
        {
            Text = name,
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center
        };
        Grid.SetRow(border, row);
        Grid.SetColumn(border, column);
        Grid.SetColumnSpan(border, columnSpan);
        parentGrid.Children.Add(border);
    }

    private static void AddGridSplitter(Grid parentGrid, int row, int column, GridResizeDirection direction)
    {
        GridSplitter splitter = new GridSplitter
        {
            Opacity = 0.5,
            ResizeDirection = direction,
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch,
            Width = direction == GridResizeDirection.Columns ? 5 : double.NaN,
            Height = direction == GridResizeDirection.Rows ? 5 : double.NaN
        };
        Grid.SetRow(splitter, row);
        Grid.SetColumn(splitter, column);
        parentGrid.Children.Add(splitter);
    }

    // 保存布局状态
    public static Dictionary<string, string> SaveLayout(Grid mainGrid)
    {
        var layoutState = new Dictionary<string, string>();

        foreach (UIElement child in mainGrid.Children)
        {
            if (child is Border border)
            {
                string regionName = ((TextBlock)border.Child).Text;
                layoutState[regionName] = $"{Grid.GetRow(border)},{Grid.GetColumn(border)},{Grid.GetColumnSpan(border)}";
            }
        }

        return layoutState;
    }

    // 恢复布局状态
    public static void RestoreLayout(Grid mainGrid, Dictionary<string, string> layoutState)
    {
        ClearMainGrid(mainGrid);

        foreach (var kvp in layoutState)
        {
            string[] values = kvp.Value.Split(',');
            int row = int.Parse(values[0]);
            int column = int.Parse(values[1]);
            int columnSpan = int.Parse(values[2]);

            CreateRegion(mainGrid, kvp.Key, row, column, columnSpan);
        }
    }
}
